package com.wbt.lettuce;

import io.lettuce.core.ClientOptions;
import io.lettuce.core.ReadFrom;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.masterreplica.MasterReplica;
import io.lettuce.core.masterreplica.StatefulRedisMasterReplicaConnection;
import io.lettuce.core.sentinel.api.StatefulRedisSentinelConnection;
import io.lettuce.core.sentinel.api.sync.RedisSentinelCommands;

public class TestSentinel {
    public static void main(String[] args) {
        RedisURI sentinelUri = RedisURI.builder()
                .withSentinel("192.168.172.201",26379)
                .withSentinel("192.168.172.202",26379)
                .withSentinel("192.168.172.203",26379)
                .withSentinelMasterId("mymaster")
                .build();
        //RedisClient redisClient = RedisClient.create("redis-sentinel://192.168.172.201:26379,192.168.172.202:26379,192.168.172.203:26379/0#mymaster");
        RedisClient redisClient = RedisClient.create(sentinelUri);
        StatefulRedisSentinelConnection<String, String> sentinelConnection = redisClient.connectSentinel();
        RedisSentinelCommands<String, String> sentinelCmd = sentinelConnection.sync();
        System.out.println(sentinelCmd.info("sentinel"));


        RedisClient client = RedisClient.create();
        ClientOptions options = ClientOptions.builder()
                .autoReconnect(true)	//是否自动重连
                .pingBeforeActivateConnection(true)	//连接激活之前是否执行PING命令
                .build();
        client.setOptions(options);
        StatefulRedisMasterReplicaConnection<String, String> conn = MasterReplica.connect(client, StringCodec.UTF8, sentinelUri);
        conn.setReadFrom(ReadFrom.ANY);

        String result = conn.sync().get("k5");
        System.out.println(result);
        conn.close();


    }
}
